package com.yao.club.utils;

import com.google.gson.Gson;


import com.yao.club.entity.result.ResponseEntity;
import org.springframework.util.StringUtils;


public class ResponseUtils {
    private static Gson gson = new Gson();


    /**
     * 成功消息
     *
     * @param object data数据
     * @param <T>    消息数据实体类型
     * @return
     */
    public static <T> ResponseEntity<T> success(T object) {
        ResponseEntity<T> responseEntity = new ResponseEntity<>();
        if (null == object) {
            responseEntity.setCode(-1);
            responseEntity.setMsg("请求失败");
            responseEntity.setData(object);
        } else {
            responseEntity.setCode(200);
            responseEntity.setMsg("请求成功");
//            if (httpServletRequest.getSession().getAttribute("disenableEncrypt").toString().equals("true")){
//
//            }
//            if (object instanceof UserEntity || object instanceof IUnionVideoSimpleEntity || object instanceof IUnionVideoMutilEntity)
//            {
//                responseEntity.setData(object);
//                return responseEntity;
//            }
//            String jsonStr = gson.toJson(object);
//            String encryStr =  AESUtils.getInstance().encrypt(jsonStr,"test");
//            responseEntity.setData((T)encryStr);

            responseEntity.setData(object);
            return responseEntity;
        }

        return responseEntity;
    }

    public static <T> ResponseEntity<T> success(T object,String message) {
        ResponseEntity<T> responseEntity = new ResponseEntity<>();
        if (null == object) {
            responseEntity.setCode(-1);
            responseEntity.setMsg("请求失败");
            responseEntity.setData(object);
        } else {
            responseEntity.setCode(200);
            responseEntity.setMsg(message);
//            if (httpServletRequest.getSession().getAttribute("disenableEncrypt").toString().equals("true")){
//
//            }
//            if (object instanceof UserEntity || object instanceof IUnionVideoSimpleEntity || object instanceof IUnionVideoMutilEntity)
//            {
//                responseEntity.setData(object);
//                return responseEntity;
//            }
//            String jsonStr = gson.toJson(object);
//            String encryStr =  AESUtils.getInstance().encrypt(jsonStr,"test");
//            responseEntity.setData((T)encryStr);

            responseEntity.setData(object);
            return responseEntity;
        }

        return responseEntity;
    }


    /**
     * 失败消息
     *
     * @param msg
     * @return
     */
    public static ResponseEntity failed(String msg) {
        ResponseEntity<String> responseEntity = new ResponseEntity<>();
        responseEntity.setCode(-1);
        responseEntity.setMsg(StringUtils.isEmpty(msg) ? "请求失败" : msg);
        responseEntity.setData("");
        return responseEntity;
    }

    /**
     * 失败消息
     *
     * @param msg
     * @return
     */
    public static ResponseEntity failed(int code,String msg) {
        ResponseEntity<String> responseEntity = new ResponseEntity<>();
        responseEntity.setCode(code);
        responseEntity.setMsg(StringUtils.isEmpty(msg) ? "请求失败" : msg);
        responseEntity.setData("");
        return responseEntity;
    }
}
